import {
    validate
} from './validate';
import {
    submit
} from './submit';
import {
    editView
} from './views/edit.view';
import{
    msg, modal
}from '../common/layer';


import {
    uploadImg
} from '../common/uploadImg';

const editBtnHandler = () => {

    let selectedList = $('#table').bootstrapTable('getSelections');
    if (selectedList.length === 0) {
        msg('请选择一项编辑');
        return;
    }

    const localData = selectedList[0];
    modal('编辑', editView(localData));


    uploadImg('#picker', '#val'); // 缩略图

    $("#picker").siblings('.list').html('<div class="file-item thumbnail" data-tips="1">' + '<img src="' + localData.val + '" class="editImg">' + '</div>');
    $("#picker").siblings('#val').val(localData.val);


    $('#submitBtn').click(() => {
        let data = {};
        $.each($('#form').serializeArray(), (key, val) => {
            data[val.name] = val.value;
        });
        if (!validate(data)) {
            return;
        }
        submit(GLOBAL_EditUrl, data);
    });

};

export {
    editBtnHandler
}